/*
 * Classname RecoverPassword.java
 *
 * Version information
 *
 * Date Mar 13, 2006
 * Copyright (c) AmSoft Systems, 2006
 */
package net.amsoft.iservice.isso.webapp.struts.action;
import java.util.Hashtable;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.amsoft.iservice.isso.service.ISSOService;
import net.amsoft.iservice.isso.util.ISSOConstants;
import net.amsoft.iservice.util.IServiceUtil;
import net.amsoft.iservice.util.ResolverClient;
import net.amsoft.iservice.util.exception.IServiceException;
import net.amsoft.iservice.isso.hibernate.Principal;
import net.amsoft.iservice.isso.hibernate.PrincipalMetaData;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 * @author mohit
 */
public class RecoverPassword extends BaseAction {
    private static final Logger oLogger = Logger
            .getLogger(RecoverPassword.class);
    public ActionForward process(ActionMapping oMapping, ActionForm oForm,
            HttpServletRequest oRequest, HttpServletResponse oResponse)
            throws Exception {
        oLogger.info("process() : entry");

        String sIName = oRequest.getParameter(ISSOConstants.REQ_PARAM_INAME);
        String sEmailAddress = oRequest
                .getParameter(ISSOConstants.REQ_PARAM_EMAIL);
        sIName = sIName.trim().toLowerCase();
        String sGlobalINumber = null;
        ActionMessages oMessages = new ActionErrors();

        //Extract, verify i-name
        sIName = IServiceUtil.getIName(sIName);
        if(sIName != null){
            sIName = sIName.toLowerCase();
        }else{
            oLogger.info("Inavlid xri="
                    + oRequest.getParameter(ISSOConstants.REQ_PARAM_INAME)
                    + " Exiting...");
            oMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                    ISSOConstants.ERR_INVALID, "i-name"));
            saveErrors(oRequest, oMessages);
            return oMapping.getInputForward();
        }

        try {
            // Resolve iname
            sGlobalINumber = ResolverClient.getGlobalINumber(sIName);
            if (sGlobalINumber == null) {
                throw new IServiceException();
            }
        } catch (Exception e) {
            oLogger.info("execute(): Message=i-name could not be resolved");
            oMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                    ISSOConstants.ERR_EMAIL_MISMATCH));
            saveErrors(oRequest, oMessages);
            return oMapping.getInputForward();

        }

        try {
            Principal oPrincipal = ISSOService.getPrincipalProfile(
                    sGlobalINumber, null);
            PrincipalMetaData oPrincipalData = ISSOService
                    .getMetaData(oPrincipal);

            // Validate if Email address is valid
            if (oPrincipalData.getEmail().equals(sEmailAddress)) {
                oLogger.debug("process() : Message : Email matches");
                oPrincipal = ISSOService.getPrincipalSecurityInfo(
                        sGlobalINumber, null);
                oPrincipalData = ISSOService.getMetaData(oPrincipal);
                oLogger.debug("process() : Message : Principal data retrieved");
                int iChallengeQuestId = oPrincipalData.getChallengeQuestionId();
                Hashtable oQuestHashTable = ISSOConstants
                        .getChallangeQuestions();
                String sChallengeQuestion = (String) oQuestHashTable.get(String
                        .valueOf(iChallengeQuestId));

                // Set request parameters
                oRequest.setAttribute(ISSOConstants.REQ_PARAM_CHALLENGE_QUEST,
                        sChallengeQuestion);
                oRequest.setAttribute(ISSOConstants.REQ_PARAM_GINU,
                        sGlobalINumber);
                oLogger.info("process() : exit");
                return oMapping.findForward(ISSOConstants.CHALLENGE_FWD);
            } else {
                // Display error on the same page
                oLogger
                        .warn("process() : exit, Message : Email does not match");
                oMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                        ISSOConstants.ERR_EMAIL_MISMATCH));
                saveErrors(oRequest, oMessages);
                return oMapping.getInputForward();
            }
        } catch (IServiceException ise) {
            oLogger.warn("process() : exit, ExceptionCode : "
                    + ise.getErrorCode() + ", ExceptionMessage : "
                    + ise.getMessage());
            oMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                    ISSOConstants.getError(ise.getErrorCode()), false));
            saveErrors(oRequest, oMessages);
            return oMapping.getInputForward();
        }
    }
}